package org.ko.core.dao.auth.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.ko.core.dao.auth.UserDao;
import org.ko.core.dao.hibernate4.BaseTypedDao;
import org.ko.core.dao.support.Finder;
import org.ko.core.domain.auth.User;
import org.ko.core.websupport.page.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;


/**
 * 系统用户管理DAO接口实现
 * @author BQC
 * @version 2014-09-14 14:15:19
 */
@Repository
public class UserDaoImpl extends BaseTypedDao<User, Long> implements UserDao{

	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
	

	public List<User> findByIds(String ids) {
		Assert.notNull(ids);
		String dic [] = StringUtils.split(ids,";");
		if(null==ids||ids.length()<1){
			return new ArrayList<User>();
		}
		String hql = "FROM User where 1=1";
		Finder finder = Finder.create(hql);
		finder.append(" and id in(").append(StringUtils.join(dic,",")).append(") ");
		return find(finder);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<User> findForSelect(int pageNo, int pageSize, String keyword) {
		String hql = "FROM User where isValid=1 and id>"+pageNo;
		Finder finder = Finder.create(hql);
		
		if(StringUtils.isNotBlank(keyword)){
			finder.append(" and (name like :kw or mobilephone like :kw or email like :kw)");
			finder.setParam("kw", "%"+keyword+"%");
		}
		finder.setMaxResults(pageSize);
		return find(finder);
	}

	
	@Override
	public Page findPage(int pageNo, int pageSize,String searchText) {
		Finder f = Finder.create("select u FROM User u where 1=1 ");
		if(StringUtils.isNotBlank(searchText)){
			f.append(" and (name like :searchText )");
			f.setParam("searchText", "%"+searchText+"%");
		}
		return find(f, pageNo, pageSize);
	}

	@Override
	public Page findPage(int pageNo, int pageSize) {
		Finder f = Finder.create("from User");
		return find(f, pageNo, pageSize);
	}
	
}